import 'package:flutter/material.dart';

/**
 * Widget TextField使用
 */
main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.teal, cardColor: Colors.orange),
      debugShowCheckedModeBanner: false,
      home: HYHomePage(),
    );
  }
}

class HYHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('基础Widge'),
      ),
      body: HYHomeContent(),
      backgroundColor: Colors.white,
      floatingActionButton: FloatingActionButton(
          child: Icon(
            Icons.add,
//            color: Colors.orange,
          ),
          backgroundColor: Colors.teal,
          onPressed: () {
            print('FloatingActionButton click');
          }),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
}

class HYHomeContent extends StatefulWidget {
  @override
  _HYHomeContentState createState() => _HYHomeContentState();
}

class _HYHomeContentState extends State<HYHomeContent> {
  final usernameTextEditController = TextEditingController();
  final passwordTextEditController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return TextFieldDemo(
        usernameTextEditController: usernameTextEditController,
        passwordTextEditController: passwordTextEditController);
  }
}

class TextFieldDemo extends StatelessWidget {
  const TextFieldDemo({
    Key key,
    @required this.usernameTextEditController,
    @required this.passwordTextEditController,
  }) : super(key: key);

  final TextEditingController usernameTextEditController;
  final TextEditingController passwordTextEditController;

  @override
  Widget build(BuildContext context) {
    return Theme(
      data: ThemeData(primaryColor: Colors.pink),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          children: [
            TextField(
              controller: usernameTextEditController,
              decoration: InputDecoration(
                  labelText: 'username',
                  icon: Icon(Icons.people),
                  hintText: '请输入用户名',
                  border: OutlineInputBorder(),
                  filled: true,
                  fillColor: Colors.black12),
              onChanged: (value) {
                print('onChanged:$value');
              },
              onSubmitted: (value) {
                print('onSubmitted:$value');
              },
            ),
            SizedBox(height: 10),
            TextField(
                controller: passwordTextEditController,
                decoration: InputDecoration(
                  labelText: 'password',
                  icon: Icon(Icons.lock),
                  border: OutlineInputBorder(),
                )),
            SizedBox(
              height: 10,
            ),
            Container(
                width: double.infinity,
                height: 40,
                child: FlatButton(
                  child: Text(
                    '登陆',
                    style: TextStyle(fontSize: 20, color: Colors.white),
                  ),
                  color: Colors.teal,
                  onPressed: () {
                    //1.获取用户名和密码
                    final username = usernameTextEditController.text;
                    final password = passwordTextEditController.text;
                    print('用户名:$username,密码:$password');
                  },
                )),
          ],
        ),
      ),
    );
  }
}
